package com.ziyun.pop.modules.storeFavouredPrice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.modules.storeFavouredPrice.dao.StoreFavouredPriceDao;
import com.ziyun.pop.modules.storeFavouredPrice.entity.StoreFavouredPriceDetailEntity;
import com.ziyun.pop.modules.storeFavouredPrice.entity.StoreFavouredPriceEntity;
import com.ziyun.pop.modules.storeFavouredPrice.service.StoreFavouredPriceDetailService;
import com.ziyun.pop.modules.storeFavouredPrice.service.StoreFavouredPriceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("storeFavouredPriceService")
public class StoreFavouredPriceServiceImpl extends ServiceImpl<StoreFavouredPriceDao, StoreFavouredPriceEntity> implements StoreFavouredPriceService {

    @Autowired
    private StoreFavouredPriceDetailService storeFavouredPriceDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<StoreFavouredPriceEntity> page = (Page<StoreFavouredPriceEntity>) this.page(
                new Query<StoreFavouredPriceEntity>(params).getPage(),
                new QueryWrapper<StoreFavouredPriceEntity>()
                        .eq("seller_id", (Long) params.get("sellerId"))
                        .eq("del_flag",0)
                        .like(params.get("favouredName")!=null,"favoured_name",params.get("favouredName"))
                        .orderBy(true, false,"create_time")
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R useRebate(Long[] favouredIds,BigDecimal rebate) {
        for (Long favouredId : favouredIds) {
            StoreFavouredPriceEntity entity = new StoreFavouredPriceEntity();
            entity.setFavouredId(favouredId);
            entity.setDiscountRate(rebate);
            this.baseMapper.updateById(entity);
        }
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R setStart(Long favouredId, Boolean value) {
        if (value) {
            StoreFavouredPriceEntity entity=new StoreFavouredPriceEntity();
            entity.setDelFlag(false);
            entity.setFavouredId(favouredId);
            this.updateById(entity);
        }else {
            StoreFavouredPriceEntity entity=new StoreFavouredPriceEntity();
            entity.setDelFlag(true);
            entity.setFavouredId(favouredId);
            this.updateById(entity);
        }
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteByIds(Long[] favouredIds,Long sellerId) {
        this.baseMapper.deleteByIds(favouredIds, sellerId);
        return R.ok();
    }

    //获取详情
    @Override
    public R detailData(Long favouredId,Long sellerId) {
        List<StoreFavouredPriceDetailEntity> storeFavouredPriceDetailEntityList = this.baseMapper.getDetailData(favouredId,sellerId);
        return R.ok().put("detailData",storeFavouredPriceDetailEntityList);
    }

    //设置阶梯折扣率
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveStepPrice(Long detailId, String stepPriceData) {
        StoreFavouredPriceDetailEntity detailEntity = new StoreFavouredPriceDetailEntity();
        detailEntity.setStepPrice(stepPriceData);
        detailEntity.setDetailId(detailId);
        boolean b = storeFavouredPriceDetailService.updateById(detailEntity);
        if (!b) {
            return R.error("阶梯折扣率设置失败!");
        }
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveDetail(StoreFavouredPriceDetailEntity storeFavouredPriceDetailEntity) {
        boolean b = storeFavouredPriceDetailService.save(storeFavouredPriceDetailEntity);
        if (!b) {
            return R.error("添加账套明细失败!");
        }
        storeFavouredPriceDetailEntity=  storeFavouredPriceDetailService.getOne(new QueryWrapper<StoreFavouredPriceDetailEntity>().orderByDesc("detail_id").last("limit 1"));
        return R.ok().put("detailId",storeFavouredPriceDetailEntity.getDetailId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteRow(Long detailId) {
        StoreFavouredPriceDetailEntity detailEntity = new StoreFavouredPriceDetailEntity();
        detailEntity.setDetailId(detailId);
        detailEntity.setDelFlag(true);
        boolean b = storeFavouredPriceDetailService.updateById(detailEntity);
        if (!b) {
            return R.error("删除账套明细失败!");
        }
        return R.ok();
    }

    @Override
    public R updateDetail(StoreFavouredPriceDetailEntity storeFavouredPriceDetailEntity) {
        boolean b = storeFavouredPriceDetailService.updateById(storeFavouredPriceDetailEntity);
        if (!b) {
            return R.error("修改账套明细失败!");
        }
        return R.ok();
    }

    @Override
    public BigDecimal getDiscountRateByMemberId(Map<String,Object> map) {
        return this.baseMapper.getDiscountRateByMemberId(map);
    }

}
